package com.longma.server.pojo.dto;

import com.longma.server.constant.enums.AdvanceQueryConditionEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;

/**
 * @author HAIBO
 * @date 2021-05-07 18:52
 * @description 高级查询实体
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AdvanceQueryConditionDTO {
    @ApiModelProperty(value = "查询字段",example="id")
    private String field;

    @NotNull(message="查询条件不能为空")
    @ApiModelProperty(value = "查询条件",example = "GE")
    private AdvanceQueryConditionEnum condition;

    @ApiModelProperty(value = "查询值",example = "0")
    private String value;

    public String changeColumnToFieldName(String columnName){
        String[] array = columnName.split("_");
        StringBuilder sb = null;
        for (String cn : array) {
            cn = cn.toLowerCase();
            if (sb == null) {
                sb = new StringBuilder(cn);
                continue;
            }
            sb.append(cn.substring(0, 1).toUpperCase()).append(cn.substring(1));
        }
        return sb== null ? null : sb.toString();
    }

    private String changeFieldToColumnName(String fieldName) {
        if (fieldName == null) {
            return null;
        }
        StringBuilder columnName = new StringBuilder();
        int length = fieldName.length();
        for (int i = 0; i < length; i++) {
            char c = fieldName.charAt(i);
            if ('A' <= c && 'Z' >= c) {
                columnName.append("_").append((char) (c + 32));
            } else {
                columnName.append(fieldName.charAt(i));
            }
        }
        return columnName.toString();
    }

    public String getColumnName(){
        return changeFieldToColumnName(this.field);
    }
}
